package com.gnerv.pabu.basic.core.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gnerv.pabu.basic.core.dto.OrgDTO;
import com.gnerv.pabu.basic.core.dto.OrgSaveDTO;
import com.gnerv.pabu.basic.core.dto.OrgSearchDTO;
import com.gnerv.pabu.basic.core.dto.OrgTreeDTO;
import com.gnerv.pabu.basic.core.dto.OrgUserDTO;
import com.gnerv.pabu.basic.core.entity.Result;
import com.gnerv.pabu.basic.core.service.IOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 基础平台-机构 controller 类
 *
 * @author zhang
 * @date 2019/11/30
 **/
@Api(tags = "机构管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/manage/org")
public class OrgController {

    @Resource
    private IOrgService orgService;

    @ApiOperation("组织机构 保存")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result saveOrg(@RequestBody OrgSaveDTO orgSaveDTO) {
        orgService.saveOrg(orgSaveDTO);
        return new Result();
    }

    @ApiOperation("组织机构 新建")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result createOrg(@RequestBody OrgSaveDTO orgSaveDTO) {
        orgService.createOrg(orgSaveDTO);
        return new Result();
    }

    @ApiOperation("组织机构 删除")
    @RequestMapping(value = "/delete/{orgId}", method = RequestMethod.GET)
    public Result deleteOrg(@PathVariable("orgId") String orgId) {
        orgService.deleteOrg(orgId);
        return new Result();
    }

    @ApiOperation("组织机构 修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateOrg(@RequestBody OrgSaveDTO orgSaveDTO) {
        orgService.updateOrg(orgSaveDTO);
        return new Result();
    }

    @ApiOperation("组织机构 状态修改")
    @RequestMapping(value = "/{orgId}/status/{status}", method = RequestMethod.GET)
    public Result updateOrgStatus(@PathVariable("orgId") String orgId, @PathVariable("status") Integer status) {
        orgService.updateOrgStatus(orgId, status);
        return new Result();
    }

    @ApiOperation("根据id 查询")
    @RequestMapping(value = "/select/{orgId}", method = RequestMethod.GET)
    public Result selectOrg(@PathVariable("orgId") String orgId) {
        OrgDTO org = orgService.selectOrg(orgId);
        return new Result().data("org", org);
    }

    @ApiOperation("根据 父id 查询 组织机构树")
    @RequestMapping(value = "/list/tree/{parentId}", method = RequestMethod.GET)
    public Result listOrgTree(@PathVariable("parentId") String parentId) {
        List<OrgTreeDTO> orgTree = orgService.listOrgTree(parentId);
        return new Result().data("orgTree", orgTree);
    }

    @ApiOperation("分页条件 查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listOrg(@RequestParam(defaultValue = "1") Integer current,
                          @RequestParam(defaultValue = "300") Integer size,
                          OrgSearchDTO orgSearchDTO) {
        Page<OrgDTO> page = orgService.listOrg(current, size, orgSearchDTO);
        return new Result().data("orgs", page);
    }

    @ApiOperation("组织机构绑定用户")
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public Result bindOrgUser(@RequestBody OrgUserDTO orgUserDTO) {
        orgService.bindOrgUser(orgUserDTO.getOrgId(), orgUserDTO.getUserIds());
        return new Result();
    }

}
